package com.lagou.service;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class RpcDecoder extends ByteToMessageDecoder{
	
	private Class<?> clazz;
	private Serializer serializer;
	
	public RpcDecoder(Class<?> clazz, Serializer serializer) {
		this.clazz = clazz;
		this.serializer = serializer;
	}

	@Override
	protected void decode(ChannelHandlerContext arg0, ByteBuf arg1, List<Object> arg2) throws Exception {
		if (clazz != null && arg1 != null) {
            int length = arg1.readInt();
            byte[] bytes = new byte[length];
            arg1.readBytes(bytes);
            Object deserialize = serializer.deserialize(clazz, bytes);
            arg2.add(deserialize);
        }
		
	}

}
